package com.ruoyi.vplat.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.vplat.domain.VplatDevice;
import com.ruoyi.vplat.service.IVplatDeviceDeptService;
import com.ruoyi.vplat.service.IVplatDeviceService;

/**
 * 设备Controller
 * 
 * @author ruoyi
 * @date 2024-04-29
 */
@RestController
@RequestMapping("/vplat/device")
public class VplatDeviceController extends BaseController
{
    @Autowired
    private IVplatDeviceService vplatDeviceService;
    @Autowired
    private IVplatDeviceDeptService vplatDeviceDeptService;
    
    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('vplat:device:list')")
    @GetMapping("/queryList")
    public TableDataInfo queryList(@RequestParam Map<String, Object> param) throws Exception
    {
		startPage();
		List<Map<String, Object>> list = vplatDeviceService.queryList(param);
		return getDataTable(list);
    }

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('vplat:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(VplatDevice vplatDevice)
    {
        startPage();
        List<VplatDevice> list = vplatDeviceService.selectList(vplatDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @PreAuthorize("@ss.hasPermi('vplat:device:export')")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VplatDevice vplatDevice)
    {
        List<VplatDevice> list = vplatDeviceService.selectList(vplatDevice);
        ExcelUtil<VplatDevice> util = new ExcelUtil<VplatDevice>(VplatDevice.class);
        util.exportExcel(response, list, "设备数据");
    }

    /**
     * 获取设备详细信息
     */
    @PreAuthorize("@ss.hasPermi('vplat:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(vplatDeviceService.selectById(id));
    }

    /**
     * 新增设备
     */
    @PreAuthorize("@ss.hasPermi('vplat:device:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VplatDevice vplatDevice)
    {
        return toAjax(vplatDeviceService.insert(vplatDevice));
    }

    /**
     * 修改设备
     */
    @PreAuthorize("@ss.hasPermi('vplat:device:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VplatDevice vplatDevice)
    {
        return toAjax(vplatDeviceService.update(vplatDevice));
    }

    /**
     * 删除设备
     */
    @PreAuthorize("@ss.hasPermi('vplat:device:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(vplatDeviceService.deleteByIds(ids));
    }
    
    /**
     * 新增设备-部门
     * @throws Exception 
     */
    @PreAuthorize("@ss.hasPermi('vplat:device:bind')")
    @Log(title = "设备-部门", businessType = BusinessType.INSERT)
    @PostMapping("/bindDept")
    public AjaxResult bindDept(@RequestBody JSONObject params) throws Exception
    {
    	return toAjax(vplatDeviceService.bindDept(params));
    }
    /**
     * 删除设备-部门
     * @throws Exception 
     */
    @PreAuthorize("@ss.hasPermi('vplat:device:bind')")
    @Log(title = "设备-部门", businessType = BusinessType.DELETE)
    @PostMapping("/unBindDept")
    public AjaxResult unBindDept(@RequestBody JSONObject params) throws Exception
    {
    	return toAjax(vplatDeviceService.unBindDept(params));
    }
}
